<?php
namespace Mobile\Controller;
use Think\Controller;

class ApiController extends Controller
{

    /**
     * 更新用户收货信息
     */
    public function updateAddress()
    {
    	$address = I('address');
    	$mobile = I('mobile');
    	$user_id = getUserId();
    	$save = array(
    		'mobile' => $mobile, 
    		'address' => $address,
    		);
        $res = M('address')->where('user_id='.$user_id)->save($save);
        if(empty($res)){
            $save['user_id'] = $user_id;
           $res = M('address')->add($save);
        }
    	echo json_encode($res);
    }
    public function getAddress()
    {
        $user_id = getUserId();
        $data = M('address')->where('user_id='.$user_id)->find();
        echo json_encode($data);
    }
    /**
     * 店面信息
     */
    public function store()
    {
        $where['s_id'] =  I('s_id');
        $where['c_id'] = I('c_id');
        $data = M('store')->field('store_name,tel,address,wechat_public,email,address_lng,address_lat,store_user,store_time,thumb,store_intro')->where($where)->find();
        $data['bannar'] = M('bannar')->field('bannar_img')->where('s_id='.$where['s_id'])->select();
        //图片的相对路径替换成为url模式
        foreach($data['bannar'] as $key=> &$value){
            $value['bannar_img'] =  str_replace("./Uploads",'http://commission.weiyuekj.com/Uploads',$value['bannar_img']);
        }
        $data['thumb'] =  str_replace("./Uploads",'http://commission.weiyuekj.com/Uploads',$data['thumb']);
        echo json_encode($data);
    }
    /**
     * 叫号接口
     */
    public function getnum()
    {
        $typeId = I('typeId');
        $sid = I('s_id');
        $key  = 'number'.$sid.'-'.$typeId;
        //缓存模板消息数据
        $bindUser['fid'] = I('fid');
        $bindUser['openid'] = getOpenid();
        S($key.'bindUser',$bindUser);
        //获取队列
        $list = S($key);
        if (!$list) {
            $list = array(1);
            $number = 1;
        }else{
            //用户的号
            $number = end($list) + 1;
            //进入队列
            $list[] = $number;
        }

        S($key, $list);
        //计算用户所在位置
        $len = count($list);

        //获取空桌子
        $key = 'empty'.$sid.'-'.$typeId;
        $countTable = S($key);
        if (!$countTable) {
            $where = array('table_cate_id' => $typeId, 's_id' => $sid );
            $countTable = M('table_cate')->where($where)->count();
            S($key,$countTable);
        }
        $countTable ? 1 : $countTable = 0;

        if ($len <= $countTable) {
            //无需等待
            echo json_encode(array('position'=>$len,'status'=>1,'number'=>$number,'s_id'=>$sid,'typeId'=>$typeId,));
        } else {
            //需等待
            echo json_encode(array('position'=>$len - $countTable ,'number'=>$number,'status'=>2,'s_id'=>$sid,'typeId'=>$typeId));
        }
    }


    /**
     * 查询队列,用户排到哪了
     */
    public function chekList()
    {
        $sid = $_GET['s_id'];
        $typeId = $_GET['typeId'];
        $number = $_GET['number'];
        $key  = 'number'.$sid.'-'.$typeId;
        //获取队列
        $list = S($key);
        $i = 0;
        $find = true;
        foreach ($list as $key => $value) {
            $i++;
            if ($value == $number) {
                $find = false;
                break;
            }
        }
        //没有找到该号,作已就位处理
        if($find){
            $i = 0;
        }
        $countTable = S('empty'.$sid.'-'.$typeId);
        $countTable ? 1 : $countTable = 0;
        if($i == 0){
            echo json_encode(array('position'=>$i,'status'=>3,'number'=>$number));
            return;
        }
        if ($i <= $countTable) {
            //无需等待
            echo json_encode(array('position'=>$i,'status'=>1,'number'=>$number));
        } else {
            //需等待
            echo json_encode(array('position'=>$i - $countTable,'number'=>$number,'status'=>2));
        }
    }

    /**
     * 餐桌分类
     */

    public function table_cate()
    {
        $sid = I('s_id');
        $table = M('table_cate')->where('s_id ='.$sid)->field('table_cate_id,table_cate_name,sit_people')->select();
        echo json_encode($table);
    }
    /**
     * 页面样式
     */
    public function storeClass()
    {   
        $sid = $_GET['shopId'];
        $data = M('store_style')->where('s_id='.$sid)->find();
        echo json_encode($data);
    }
}
